package com.idbk.chargestation.activity.pile;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.idbk.chargestation.R;
import com.idbk.chargestation.api.ChargeStationAPI;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonBase;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.Const;
import com.idbk.chargestation.util.GsonUtils;

/**
 * 充电点投诉  详情输入
 * @author lupc
 *
 */
public class ActivityComplainInfo extends BaseActivity implements OnClickListener {
	
	public final static String KEY_TITLE = "key_title";
	public final static String KEY_COMPLAIN_TYPE = "key_complain_type";
	
	private Context mContext;
	
	private int mPointId;
	
	private int mComplainEnum;
	
	private EditText mEditComplainOther;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_complain_info);
		
		mContext = this;
		
		setupView();
		
		setupData();
	}
	
	private void setupView(){				
		setupToolBar2();
		String title = getIntent().getStringExtra(KEY_TITLE);
		if(title != null){
			setToolbar2Title(title);
		}
		
		findViewById(R.id.textview_submit).setOnClickListener(this);
		
		mEditComplainOther = (EditText)findViewById(R.id.edittext_complain_other);
		
	}
	
	private void setupData(){
		mPointId = getIntent().getIntExtra(Const.KEY_ID, 0);
		
		mComplainEnum = getIntent().getIntExtra(KEY_COMPLAIN_TYPE, -1);
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.textview_submit){
			String other = mEditComplainOther.getText().toString();
			if(checkInput(other)){
				showMyProgressDialog(true,true,"提交中...");
				mMainRequest = ChargeStationAPI.complain(mComplainEnum, other, mPointId, mResponse);
			}			
		}
	}
	
	private boolean checkInput(String other){
		if(mComplainEnum < 1 || mComplainEnum > 5 || mPointId < 1){
			Toast.makeText(mContext, "参数错误！", Toast.LENGTH_SHORT).show();
			return false;
		}
		if(mComplainEnum == 5 && (other == null || other.length() < 1)){
			Toast.makeText(mContext, "此条件下你必须输入描述信息！", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}
	
	private final EHttpResponse mResponse = new EHttpResponse() {
		
		@Override
		public void onEResponse(String response) {
			JsonBase result = GsonUtils.toBean(JsonBase.class, response);
			if(handleResponseStatus(result)){
				Toast.makeText(mContext, "提交成功", Toast.LENGTH_SHORT).show();
				finish();
			}
		}

		@Override
		public void onEErrorResponse(VolleyError error) {
			super.onEErrorResponse(error);
			Toast.makeText(mContext,
					R.string.common_tip_resultfail,
					Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onEFinish() {
			super.onEFinish();
			dismissMyProgressDialog();
		}
		
	};
	
}
